package cn.virens.web.controller.open.wxmp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.virens.common.RequestUtil;
import cn.virens.web.common.BaseJSONController;
import cn.virens.web.components.open.wxmp.oauth.WxMpOauthSevice;

@Controller
@RequestMapping("open/wxmp/{appid}/oauth")
public class WxMpOauthContorller extends BaseJSONController {
	private static final String callback = "%s/open/wxmp/%s/oauth/callback";

	private @Autowired WxMpOauthSevice wxMpOauthSevice;

	/**
	 * 请求前置，将对应微信的appid作为当前作用域的默认配置key
	 * 
	 * @param appid 微信的appid
	 */
	@ModelAttribute
	public void init(@PathVariable("appid") String appid) {
		this.wxMpOauthSevice.switchoverTo(appid);
	}

	@RequestMapping(value = "authorize", method = RequestMethod.GET)
	public String authorize(@PathVariable("appid") String appid, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String redirectURI = String.format(callback, RequestUtil.getRequestURI(request), appid);
		String targetURI = RequestUtil.getValueStr(request, "url");

		return redirect(wxMpOauthSevice.oauth2BuildAuthURI(redirectURI, targetURI));
	}

	@RequestMapping(value = "callback", method = RequestMethod.GET)
	public String callback(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String state = RequestUtil.getValueStr(request, "state");
		String code = RequestUtil.getValueStr(request, "code");

		return redirect(wxMpOauthSevice.oauth2GetTargetURI(code, state));
	}

	@ResponseBody
	@RequestMapping(value = "userinfo.json", method = RequestMethod.GET)
	public Model userinfo(@RequestParam("state") String state, Model model) throws Exception {
		return renderSuccess(wxMpOauthSevice.oauth2GetUserinfo(state), model);
	}

	@ResponseBody
	@RequestMapping(value = "jsapi.json", method = RequestMethod.GET)
	public Model jsapiSignature(@RequestParam("url") String url, Model model) throws Exception {
		return renderSuccess(wxMpOauthSevice.signJsapi(url), model);
	}

	@RequestMapping(value = "vue/{version}.jspx", method = RequestMethod.GET)
	public String vue(@PathVariable("version") String version, HttpServletRequest request, Model model) throws Exception {
		model.addAttribute("state", RequestUtil.getValueStr(request, "_state_"));
		model.addAttribute("version", version);

		return returnUrl("/portal/vue");
	}

	private static String redirect(String redirectURI) {
		return "redirect:" + redirectURI;
	}
}
